package dao

import (
	"goWeb/bookStore/model"
	"goWeb/bookStore/utils"
)

//添加订单
func AddOrder(order *model.Order) (err error) {
	sqlstr := "insert into orders values(?,?,?,?,?,?)"
	_, err = utils.Db.Exec(sqlstr, order.Id, order.CreateTime, order.Tcount, order.Tamout, order.Statu, order.UserId)
	return
}

//获取所有订单
func GetAllOrders() (orders []*model.Order, err error) {
	sqlstr := "select id,create_time,tcount,tamout,statu,userid from orders"
	rows, err := utils.Db.Query(sqlstr)
	for rows.Next() {
		order := &model.Order{}
		rows.Scan(&order.Id, &order.CreateTime, &order.Tcount, &order.Tamout, &order.Statu, &order.UserId)
		orders = append(orders, order)
	}
	return
}

//根据 用户ID获取订单
func GetMyOrder(Userid int) (orders []*model.Order, err error) {
	sqlstr := "select id,create_time,tcount,tamout,statu,userid from orders where userid=?"
	rows, err := utils.Db.Query(sqlstr, Userid)
	for rows.Next() {
		order := &model.Order{}
		rows.Scan(&order.Id, &order.CreateTime, &order.Tcount, &order.Tamout, &order.Statu, &order.UserId)
		orders = append(orders, order)
	}
	return
}

//更新订单的状态
func UpdateOrderStatusById(orderid string, status int) (err error) {
	sqlstr := "update orders set statu=? where id=?"
	_, err = utils.Db.Exec(sqlstr, status, orderid)
	return err
}
